using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoMBot.MemoryLib;

namespace RoMBot.Classes
{
    public class Pet : Pawn
    {
        private static Dictionary<PawnClass, FairyDef> FairyDb = new Dictionary<PawnClass, FairyDef>();


        static Pet()
        {
            FairyDb.Add( PawnClass.Warrior, new FairyDef { PetId = 102104, PetSummon = 493267, Buff = 503455 } );
            FairyDb.Add( PawnClass.Scout, new FairyDef { PetId = 102105, PetSummon = 493268, Buff = 503736 } );
            FairyDb.Add( PawnClass.Rogue, new FairyDef { PetId = 102106, PetSummon = 493268, Buff = 503459 } );
            FairyDb.Add( PawnClass.Mage, new FairyDef { PetId = 102107, PetSummon = 493270, Buff = 503461 } );
            FairyDb.Add( PawnClass.Knight, new FairyDef { PetId = 102108, PetSummon = 493268, Buff = 503507 } );
        }

        public Pet( int address )
            : base( address )
        {
        }

        public override void Update()
        {
            //if ( !HasAddress )
            {
                Address = Memory.ReadRepeat<int>( Bot.Instancia.ProcId, Bot.Player.Address + Addresses.pawnPetPtr_offset );
            }
            base.Update();
            SetAutoAttacks();
        }

        private bool autoAttacksSet;

        public void SetAutoAttacks()
        {
            if ( autoAttacksSet )
            {
                return;
            }
            if ( Id == 102297 )
            {
                //icon,active,autoCastAllowed
                var resu = Helpers.RoMScript( "GetPetActionInfo(4)" ) as List<object>;
                if ( resu != null )
                {
                    if ( !( bool ) resu[ 1 ] && ( bool ) resu[ 2 ] )
                    {
                        Helpers.RoMScript( "UsePetAction(4)" );
                    }
                }
                resu = Helpers.RoMScript( "GetPetActionInfo(5)" ) as List<object>;
                if ( resu != null )
                {
                    if ( !( bool ) resu[ 1 ] && ( bool ) resu[ 2 ] )
                    {
                        Helpers.RoMScript( "UsePetAction(5,true)" );
                    }
                }
            }
            autoAttacksSet = true;
        }

        public void StartCombat()
        {
            if ( this.Id == 102325 ) // Nature Crystal
            {
                Bot.Print( "Nature Crystal Doesn't attack" );
                return;
            }

            Helpers.RoMScript( "UsePetAction(3)" );
            Bot.Print( "Making Pet Attack" );
        }

        public override string ToString()
        {
            return string.Format( "Pet - name: {0}", Name );
        }
    }

    public class FairyDef
    {
        public int PetId { get; set; }
        public int PetSummon { get; set; }
        public int Buff { get; set; }
    }
}
